//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client;

import tv.dyndns.kishibe.client.packet.PacketProblemData;
import tv.dyndns.kishibe.client.ui.PageSelectable;
import tv.dyndns.kishibe.client.ui.WidgetPageSelector;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class WidgetProblemReport extends VerticalPanel implements ClickHandler, PageSelectable {
	private final Button buttonRegisterAll = new Button("全ての問題を登録する", this);
	private final ProblemReportManager manager;
	private final WidgetPageSelector pageSelector;
	private final SimplePanel panelProblemReport = new SimplePanel();
	private GridProblemReport gridProblemReport;
	private int selectedPage = 0;
	private final boolean regist;
	private final int maxProblemsPerPage;

	public WidgetProblemReport(PacketProblemData[] problems, boolean regist, boolean initialSort, int maxProblemsPerPage) {
		this.pageSelector = new WidgetPageSelector(this, ((problems.length - 1) / maxProblemsPerPage) + 1);
		this.regist = regist;
		this.maxProblemsPerPage = maxProblemsPerPage;

		manager = new ProblemReportManager(problems);

		setHorizontalAlignment(ALIGN_CENTER);

		final int count = problems.length;
		add(new HTML(count + "件ヒット"));
		add(pageSelector);
		add(panelProblemReport);

		if (regist) {
			add(buttonRegisterAll);
		}

		if (initialSort) {
			manager.sort(ProblemReportManager.SORT_BY_PROBLEM_ID);
		}

		pageSelector.setPage(0);
	}

	public void sort(int sortBy) {
		manager.sort(sortBy);
		pageSelector.setPage(0);
	}

	private void registerAllProblems() {
		gridProblemReport.disableRegisterButton();
		buttonRegisterAll.setEnabled(false);

		final PacketProblemData[] problems = manager.getProblems();
		final int userCode = UserData.getInstance().getUserCode();

		final int[] problemIds = new int[problems.length];
		for (int i = 0; i < problems.length; ++i) {
			problemIds[i] = problems[i].problemId;
		}

		Service.Util.getInstance().addProblemIdsToReport(userCode, problemIds, Service.NULL_CALLBACK);
	}

	/**
	 * 表示するページを指定する。
	 * 
	 * @param page
	 *            ページ番号(0ベース)
	 */
	public void selectPage(int page) {
		selectedPage = page;

		final PacketProblemData[] problems = manager.getProblems();
		final int beginIndex = maxProblemsPerPage * selectedPage;
		final int endIndex = Math.min(maxProblemsPerPage * (selectedPage + 1), problems.length);

		gridProblemReport = new GridProblemReport(this, problems, beginIndex, endIndex, regist);
		panelProblemReport.setWidget(gridProblemReport);
	}

	@Override
	public void onClick(ClickEvent event) {
		final Widget sender = (Widget) event.getSource();
		if (sender == buttonRegisterAll) {
			registerAllProblems();
		}
	}
}
